package com.zwy.customseekbar

import android.os.Bundle
import android.widget.SeekBar
import android.widget.TextView
import androidx.activity.ComponentActivity

class MainActivity : ComponentActivity() {

    private var seekBar: SeekBar? = null
    private var textView: TextView? = null

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        seekBar = findViewById(R.id.seekbar)
        textView = findViewById(R.id.seekbar_value_tv)

        seekBar!!.setOnSeekBarChangeListener(object : SeekBar.OnSeekBarChangeListener {
            override fun onProgressChanged(seekBar: SeekBar?, progress: Int, fromUser: Boolean) {
                updateProgressTextPosition()
                textView!!.text = "$progress℃"
            }

            override fun onStartTrackingTouch(seekBar: SeekBar?) {
                // 开始拖动时的操作
            }

            override fun onStopTrackingTouch(seekBar: SeekBar?) {
                // 停止拖动时的操作
            }
        })
    }

    private fun updateProgressTextPosition() {
        val thumb = seekBar!!.thumb
        val thumbBounds = thumb.bounds
        val thumbCenterX = thumbBounds.centerX()

        // 将 thumb 的中心点作为文本的位置
        textView!!.x = thumbCenterX.toFloat() - textView!!.width / 2 + 100
    }
}
